package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sport;
import com.itheima.service.FoodKnowledgeService;
import com.itheima.service.SportKnowledgeService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xw
 * @date 2020-08-22 20:04
 */
@RestController
@RequestMapping("/sportknowledge")
public class SportKnowledgeController {

    @Reference
    private SportKnowledgeService sportKnowledgeService;

    //分页
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult pageResult=sportKnowledgeService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Sport sport){
        try {
            sportKnowledgeService.add(sport);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }

    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            sportKnowledgeService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    //查询单个
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable int id) {
        try {
            //查询检查项
            Sport sport=sportKnowledgeService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑
    @PutMapping("/edit")
    public Result edit(@RequestBody Sport sport){
        try {
            sportKnowledgeService.edit(sport);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    //上传
    @RequestMapping("/upload3")
    public Result upload2(@RequestParam("sport") MultipartFile excelFile){
        try {
            List<String[]> list= POIUtils.readExcel(excelFile);
            List<Sport> data=new ArrayList<>();
            for (String[] strings : list) {
                String sportname = strings[0];
                String sportrisk = strings[1];
                String sportkind = strings[2];
                String sporttime = strings[3];
                String sportfrequency = strings[4];
              Sport sport=new Sport();
                data.add(sport);
            }
            sportKnowledgeService.adds(data);

            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (IOException e){
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


}